//
//  StoreManagerViewController.m
//  iStore
//
//  Created by MacDev on 15/4/16.
//  Copyright (c) 2015年 mirrtalk. All rights reserved.
//

#import "StoreManagerViewController.h"
#import "LocationViewController.h"
#import "StoreManagerTableViewCell.h"
#import "MyUtil.h"
#import "AddStoreViewController.h"
#import "RequestEngine.h"

@interface StoreManagerViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSString *shopID;
    NSInteger indexSection;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
//用来存放所有的数据
@property (nonatomic, retain) NSMutableArray *arrayDatas;


@end

@implementation StoreManagerViewController
{

    StoreManagerTableViewCell * _cell;

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _arrayDatas = [[NSMutableArray alloc]init];
    self.title = @"店铺管理";
    _tableView.tableFooterView = [[UIView alloc]init];
    self.automaticallyAdjustsScrollViewInsets = false;
    _tableView.allowsSelection = NO;
    
    NSString *userID = [[NSUserDefaults standardUserDefaults]objectForKey:kUserId];
    [self activityStart];
    [RequestEngine RequestEngineWithDic:@{@"userID":userID} URL:@"/shop/getMyShops" completed:^(NSString *errorcode, NSDictionary *resultDict) {
        [self activityStop];
        if ([errorcode isEqualToString:@"0"]) {
            for (id obj in resultDict) {
                [_arrayDatas addObject:obj];
            }
            [_tableView reloadData];
        }
        if (_arrayDatas.count == 0) {
            alert(@"亲！您现在尚未注册添加店铺信息或您添加的店铺信息正在审核中暂不能进行广告投放操作");
        }
    }];
    
    
    [_add setBackgroundImage:[UIImage imageNamed:@"Rectangle_6"] forState:UIControlStateHighlighted];
}


#pragma mark -- 表的设置 --
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _arrayDatas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * indentifer = @"Cell";
    _cell = [tableView dequeueReusableCellWithIdentifier:indentifer];
    if (!_cell)
    {
        _cell = [[[NSBundle mainBundle]loadNibNamed:@"StoreManagerTableViewCell" owner:self options:nil]firstObject];
    }

    [_cell refreshData:[_arrayDatas objectAtIndex:indexPath.section]];
    return _cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

#pragma mark --添加店铺按钮--
- (IBAction)addStore:(UIButton *)sender
{
  AddStoreViewController   * vc = [[AddStoreViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark -- 删除---
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    shopID = [_arrayDatas objectAtIndex:indexPath.section][@"id"];
    indexSection = indexPath.section;
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确定撤销吗？" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    [alert show];
    
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 0) {
        //1.删除数据源中指定位置的数据
        NSDictionary *diction =@{@"shopID":shopID};
        [_arrayDatas removeObjectAtIndex:indexSection];
        [_tableView deleteSections:[NSIndexSet indexSetWithIndex:indexSection] withRowAnimation:UITableViewRowAnimationAutomatic];
        
        [self activityStart];
        [RequestEngine RequestEngineWithDic:diction URL:@"/shop/deleteShop" completed:^(NSString *errorcode, NSDictionary *resultDict) {
            [self activityStop];
            if ([errorcode isEqualToString:@"0"]) {
                alert(@"删除成功");
            }
            else
                alert(@"删除失败");
        }];
    }
    else {
        return;
    }
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    [_tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end